package org.amphiprion.descentv2assistant.ui.fragment;

import java.util.Collections;
import java.util.List;

import org.amphiprion.descentv2assistant.App;
import org.amphiprion.descentv2assistant.GameSessionActivity;
import org.amphiprion.descentv2assistant.MainActivity;
import org.amphiprion.descentv2assistant.R;
import org.amphiprion.descentv2assistant.adapter.SlidingMenuAdapter;
import org.amphiprion.descentv2assistant.dao.db.GameSessionDao;
import org.amphiprion.descentv2assistant.entity.GameSession;
import org.amphiprion.descentv2assistant.ui.views.controler.UndoBarController;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class MenuFragment extends SherlockFragment implements View.OnClickListener, SlidingMenuAdapter.OnEditModeListener, UndoBarController.UndoListener, SlidingMenuAdapter.OnRemoveListener {
	private View fragment;
	public static MenuFragment sThis;
	private SlidingMenuAdapter mAdapter;
	private TextView mEdit;
	private LayoutInflater mInflater;
	private ListView mListView;
	private boolean mShouldRefreshCards;
	private List<GameSession> gameSessions;
	private UndoBarController mUndoBarController;

	private ActionMode.Callback mActionModeCallback;
	private ActionMode mActionMode;
	private GameSession current;

	private void initLayout() {
		this.mListView = ((ListView) this.fragment.findViewById(R.id.gameSessionList));
		this.mListView.setEmptyView(this.fragment.findViewById(android.R.id.empty));
		this.mEdit = ((TextView) this.fragment.findViewById(R.id.edit));
		this.mEdit.setOnClickListener(this);

		mActionModeCallback = new ActionMode.Callback() {

			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				return false;
			}

			@Override
			public void onDestroyActionMode(ActionMode mode) {
				mActionMode = null;
			}

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.context_game_session, menu);
				return true;
			}

			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
				case R.id.edit:
					mode.finish(); // Action picked, so close the CAB
					MainActivity.sThis.onAddEditGameSession(current);
					// shareCurrentItem();
					return true;
				default:
					return false;
				}
			}
		};
	}

	public static MenuFragment newInstance() {
		return new MenuFragment();
	}

	private void onLoadDone() {
		if (this.gameSessions.isEmpty()) {
			this.mEdit.setVisibility(View.GONE);
		} else {
			this.mEdit.setVisibility(View.VISIBLE);
		}

		boolean oldEditMode = false;
		if (this.mAdapter != null) {
			oldEditMode = mAdapter.isEditMode();
		}
		this.mAdapter = new SlidingMenuAdapter(getSherlockActivity(), this.gameSessions);

		this.mListView.setAdapter(this.mAdapter);
		this.mAdapter.setEditListner(this);
		this.mAdapter.setUndoController(this.mUndoBarController);
		this.mAdapter.setOnRemoveListener(this);
		if (oldEditMode) {
			toggleEdit();
		}
	}

	@Override
	public void hideShow(int index) {
		GameSession localGameSessions = this.mAdapter.remove(index);
		GameSessionDao.getInstance(getActivity()).setVisibility(false, localGameSessions.getId());
		// this.mDB.setTVShowVisibility(false, localSeries.getSeriesId());
		this.mUndoBarController.showUndoBar(true, localGameSessions.getName(), localGameSessions, index);
		this.mShouldRefreshCards = true;
	}

	public void initData() {
		this.gameSessions = GameSessionDao.getInstance(this.getActivity()).getAllVisible();
		Collections.sort(this.gameSessions);
		onLoadDone();
	}

	@Override
	public void onActivityCreated(Bundle paramBundle) {
		super.onActivityCreated(paramBundle);
		initLayout();
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.edit:
			toggleEdit();
			return;
		}

	}

	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		sThis = this;
	}

	@Override
	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
		this.mInflater = paramLayoutInflater;
		this.fragment = this.mInflater.inflate(R.layout.fragment_main_menu, paramViewGroup, false);
		this.mUndoBarController = new UndoBarController(this.fragment.findViewById(R.id.undobar), this);

		return this.fragment;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		sThis = null;
	}

	@Override
	public void onEditMode(boolean editMode) {
		if (editMode) {
			this.mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View view, int index, long paramAnonymousLong) {
					MainActivity.sThis.onAddEditGameSession(MenuFragment.this.gameSessions.get(index));
				}
			});
			this.mListView.setOnItemLongClickListener(null);
			// this.mListView.setOnTouchListener(this.touchListener);
			// this.mListView.setOnScrollListener(this.touchListener.makeScrollListener());

		} else {
			this.mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View view, int index, long paramAnonymousLong) {
					Intent intent = null;

					intent = new Intent(getActivity(), GameSessionActivity.class);

					App.currentGameSession = MenuFragment.this.gameSessions.get(index);
					getActivity().startActivity(intent);

					MainActivity.sThis.toggle();

				}
			});
			this.mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
				@Override
				public boolean onItemLongClick(AdapterView<?> paramAnonymousAdapterView, View view, int index, long paramAnonymousLong) {
					if (mActionMode != null) {
						return false;
					}
					current = MenuFragment.this.gameSessions.get(index);
					// Start the CAB using the ActionMode.Callback defined above
					mActionMode = getSherlockActivity().startActionMode(mActionModeCallback);
					view.setSelected(true);
					return true;
				}
			});

			this.mListView.setOnTouchListener(null);
			this.mListView.setOnScrollListener(null);

		}
		fragment.postInvalidate();
	}

	@Override
	public void onResume() {
		super.onResume();
		initData();
	}

	@Override
	public void onUndo(Object paramObject, int index) {
		GameSession localGameSession = (GameSession) paramObject;
		GameSessionDao.getInstance(getActivity()).setVisibility(true, localGameSession.getId());
		this.gameSessions.add(index, localGameSession);
		mAdapter.notifyDataSetChanged();
		this.mShouldRefreshCards = true;
	}

	public void setUpdndexg(int paramInt) {
		this.mAdapter.setUpdating(paramInt);
	}

	public boolean shouldRefreshCards() {
		return this.mShouldRefreshCards;
	}

	public void toggleEdit() {
		toggleEdit(!this.mAdapter.isEditMode());
	}

	public void toggleEdit(boolean editMode) {

		this.mAdapter.toggleEdit(editMode);
		if (editMode) {

			// AppMsg appMsg = AppMsg.makeText(getSherlockActivity(),
			// R.string.swipe_to_remove, AppMsg.STYLE_CONFIRM)
			// .setFragment(this);
			// appMsg.setLayoutGravity(Gravity.BOTTOM);
			// appMsg.show();
			this.mEdit.setText(R.string.done);
			mEdit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_tick_small, 0, 0, 0);
		} else {
			this.mEdit.setText(R.string.edit);
			mEdit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_action_edit_small, 0, 0, 0);
		}

	}

}